use cli::cnf;
use log::debug;
use pretty_env_logger;
use std::future::Future;
use std::process::ExitCode;
mod cli;
pub mod fund;
pub mod helper;
pub mod kzz;
pub mod stock;
pub mod index;
pub mod todo;
pub mod qtrade;

fn main() -> ExitCode {
    pretty_env_logger
        ::formatted_timed_builder()
        .filter_module("surrealdb_core", log::LevelFilter::Info)
        .filter_module("fuda", log::LevelFilter::Info)
        .build();

    //    ::from_env(env_logger::Env::default().default_filter_or("info"))
    //    .filter_module("surrealdb_core", log::LevelFilter::Info)
    //    // .filter_module("fuda", log::LevelFilter::Info)
    //    .init();
    // Initialize the logger with the RUST_LOG environment variable
    pretty_env_logger::init_timed();

    // Initiate the command line
    with_enough_stack(cli::init())
}

/// Rust's default thread stack size of 2MiB doesn't allow sufficient recursion depth.
fn with_enough_stack<T>(fut: impl Future<Output = T> + Send) -> T {
    debug!("Starting Tokio runtime RUNTIME_STACK_SIZE={}", *cnf::RUNTIME_STACK_SIZE);
    // Start a Tokio runtime with custom configuration
    tokio::runtime::Builder
        ::new_multi_thread()
        .enable_all()
        .max_blocking_threads(*cnf::RUNTIME_MAX_BLOCKING_THREADS)
        .thread_stack_size(*cnf::RUNTIME_STACK_SIZE)
        .thread_name("fudacraw-worker")
        .build()
        .unwrap()
        .block_on(fut)
}
